package com.jn.okhttp.views;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jn.okhttp.R;
import com.jn.okhttp.adapter.HotSearchAdapter;
import com.jn.okhttp.entity.HotSearchItem;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HotSearchListActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private HotSearchAdapter adapter;
    private List<HotSearchItem> itemList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hot_search_list);

        recyclerView = findViewById(R.id.recycler_view);
        itemList = new ArrayList<>();

        // 替换为实际的 API 地址
        String apiUrl = "https://api.oioweb.cn/api/getBingImage"; // 示例 URL
        fetchData(apiUrl);
    }

    private void fetchData(String url) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("HotSearchListActivity", "请求失败: ", e);
                runOnUiThread(() -> Toast.makeText(HotSearchListActivity.this, "请求失败", Toast.LENGTH_SHORT).show());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String responseData = response.body().string();
                    parseData(responseData); // 解析数据
                } else {
                    Log.e("HotSearchListActivity", "请求失败: " + response.message());
                }
            }
        });
    }

    private void parseData(String jsonData) {
        try {
            JSONObject jsonObject = new JSONObject(jsonData);
            int code = jsonObject.getInt("code");
            if (code == 200) {
                JSONArray results = jsonObject.getJSONArray("result");
                for (int i = 0; i < results.length(); i++) {
                    JSONObject item = results.getJSONObject(i);
                    String copyright = item.getString("copyright");
                    String copyrightLink = item.getString("copyrightlink");
                    String title = item.getString("title");
                    String imageUrl = item.getString("url");

//                    HotSearchItem hotSearchItem = new HotSearchItem(title, copyright, copyrightLink, imageUrl);
//                    itemList.add(hotSearchItem);
                }
                runOnUiThread(this::setupRecyclerView);
            }
        } catch (JSONException e) {
            Log.e("HotSearchListActivity", "解析数据失败: ", e);
        }
    }

    private void setupRecyclerView() {
        adapter = new HotSearchAdapter(itemList, this);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }
}
